package com.mytest.leetcode;

import lombok.extern.slf4j.Slf4j;

import java.util.Deque;
import java.util.LinkedList;

@Slf4j
public class Solution739 {

    public static void main(String[] args) {
        Solution739 solution704 = new Solution739();
        int[] nums = {73,74,75,71,69,72,76,73};
        log.info("{}", solution704.dailyTemperatures(nums));
    }

    /**
     * 单调栈，先今后出。push 进，pop出最后一次push的
     * @param temperatures
     * @return
     */
    public int[] dailyTemperatures(int[] temperatures) {
        int length = temperatures.length;
        int ans [] = new int[length];
        Deque<Integer> stack = new LinkedList<>();
        for (int i=0;i< length;i++) {
            int temp = temperatures[i];
            while(!stack.isEmpty() && temp > temperatures[stack.peek()]){
                int value = stack.pop();
                ans[value] = i - value;
            }
            stack.push(i);
        }
        return ans;
    }

}
